package jp.co.sharp.android.decome;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.res.Resources;
import android.graphics.Canvas;
import dalvik.system.DexClassLoader;

public class Reflector {
	static File file = null;

	public static void setFilesDir(File file1) {
		file = file1;
	}

	private static File getFilesDir() {
		return file;
	}

	public static Object load(Resources res, byte[] dexCode) {
		try {
			final ClassLoader classLoader = new DexClassLoader(getFilesDir()
					.getAbsolutePath() + "/animation.jar", getFilesDir()
					.getAbsolutePath(), null,
					ClassLoader.getSystemClassLoader());
			{
				Class<?> a = classLoader
						.loadClass("jp.co.sharp.android.decome.aobject.AHandler");
				try {
					Method meth = a.getMethod("setRes",
							new Class[] { DexClassLoader.class });
					meth.invoke(null, classLoader);
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
			Class<?> a = classLoader
					.loadClass("jp.co.sharp.android.decome.aobject.Rotation");
			Object handler = a.newInstance();
			return handler;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static int nextTime(Object handler) {
		try {
			Method meth = handler.getClass().getMethod("nextTime",
					(Class[]) null);
			Integer ret = (Integer) meth.invoke(handler);
			return ret.intValue();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static void next(Object handler, Canvas canvas) {
		try {
			Method meth = handler.getClass().getMethod("next",
					new Class[] { Canvas.class });
			meth.invoke(handler, canvas);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
}
